import api from '@/api'
import { deepCopy, setPage } from '@/utils/util'

const initPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 0,
    filter: {
        isAdmin: true,
        orderbyList: {},
        roleList: []
    }
}

const state = {
    data: {
        users: [],
        organizations: []
    },

    ui: {
        page: deepCopy(initPage)
    }
}

const getters = {
    users: state => state.data.users,
    organizations: state => state.data.organizations,
    usersTotal: state => state.ui.page.total
}

// actions
const actions = {
    getUsers({ commit }, params) {
        api.getUsers({
            loading: true,
            method: 'post',
            data: params ? params : state.ui.page,
            success: res => {
                const { pages, total } = res
                commit('SETUSERPAGE', { pages, total })
                let users = []
                if (res.content && res.content.length) {
                    users = res.content.map(item => {
                        // 部门名称处理
                        if (item.organizationTitles && item.organizationTitles.length) {
                            item['organizationTitle'] = item.organizationTitles.join('/')
                        }
                        return item
                    })
                }
                console.log('--user--')
                console.log(users)
                console.log('--user--')
                commit('SETUSERS', { users })
            }
        })
    },

    addUser({ dispatch }, params) {
        api.addUser({
            loading: true,
            method: 'put',
            data: params,
            success: res => {
                dispatch('getUsers')
            }
        })
    },

    editUser({ dispatch }, params) {
        api.editUser({
            loading: true,
            method: 'put',
            data: params,
            success: res => {
                dispatch('getUsers')
            }
        })
    },

    removeUser({ dispatch }, params) {
        api.removeUser({
            loading: true,
            method: 'post',
            data: params,
            success: res => {
                dispatch('getUsers')
            }
        })
    },

    resetUserPassword({ dispatch }, data) {
        api.resetUserPassword(data, {
            success: () => {
                dispatch('getUsers')
            }
        })
    },

    getOrganizations({ commit }, params) {
        api.getOrganizations({
            method: 'get',
            data: params,
            success: res => {
                let organizations = res
                commit('SETORGANIZATIONS', { organizations })
            }
        })
    }
}

// mutations
const mutations = {
    SETUSERS(state, payload) {
        state.data.users = payload.users
    },

    SETORGANIZATIONS(state, payload) {
        state.data.organizations = payload.organizations
    },

    SETUSERPAGE(state, page) {
        page && setPage(state.ui.page, page)
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
